import { Ast, createRootAst } from "./ast";
import { BaseVisitor } from "./core";
import { toHomeJingXuan, toTabLayout } from "./to";
import { swipeUpV3 } from "./utils";
class YangJiContext {
    name: string;
    label: string;
    live: string;
    type: string;
    toLive: () => void;
    action_bar: string;
    close: () => void;
    like: () => void;
}
class YangJiVisitor extends BaseVisitor {
    visitTextView(node: Ast, ctx: YangJiContext) {
        if (node.id.endsWith(`user_name_text_view`)) {
            ctx.name = node.text;
        }
        if (node.id.endsWith(`user_name_text_view_new`)) {
            ctx.name = node.text;
        }
        if (node.id.endsWith(`element_caption_label`)) {
            ctx.label = node.text;
        }
        if (node.id.endsWith(`live_preview_bottom_caption`)) {
            ctx.label = node.text;
        }
        if (node.id.endsWith(`live_cover_icon_text`)) {
            ctx.live = node.text;
        }
        if (node.id.endsWith(`live_simple_play_swipe_text`) && node.clickable) {
            ctx.type = `直播`
            ctx.toLive = () => node.click()
        }
        if (node.text === `上滑继续观看视频`) {
            // 调查
            ctx.type = `调查`
        }
    }
    visitFrameLayout(node: Ast, ctx: YangJiContext) {
        if (node.id.endsWith(`like_button`) && node.clickable && node.visibleToUser) {
            ctx.like = () => node.click()
            return;
        }
        node.children.map(c => c.visit(this, ctx))
    }
    visitViewGroup(node: Ast, ctx: YangJiContext) {
        if (node.desc === `action_bar_view`) {
            ctx.type = `推广`
            ctx.action_bar = node.children.map(c => c.text).join(' ')
        }
        if (node.desc === `close_view` && node.clickable && node.visibleToUser) {
            ctx.close = () => node.click()
            ctx.type = `广告`
        }
    }
}

export class AutoYangJiTask {
    runing: boolean = true;
    keyword: string = ``;
    timeout: number = 1000 * 60 * 1;
    onceTimeout: number = 1000 * 30;
    startTime: number = new Date().getTime();
    onceStartTime: number = new Date().getTime();
    constructor(private that?: any){}
    logger: (text: string) => void;
    isFinish() {
        if ((new Date().getTime() - this.startTime) >= this.timeout) {
            return true;
        }
        return false;
    }
    isOnceFinish() {
        if ((new Date().getTime() - this.onceStartTime) >= this.onceTimeout) {
            return true;
        }
        return false;
    }
    getKeywords() {
        return this.keyword.split('#').filter(it => !!it)
    }
    once() {
        const keywords = this.getKeywords()
        const ctx = new YangJiContext()
        while (!this.isOnceFinish() && !this.isFinish() && this.runing && this.getOtherCondiction()) {
            const visitor = new YangJiVisitor();
            const root = createRootAst()
            root.visit(visitor, ctx)
            if (ctx.label && ctx.like) {
                break;
            }
            sleep(1000)
        }
        this.logger(`【${ctx.name}】正在观看`)
        const hasUse = new Date().getTime() - this.onceStartTime;
        if (hasUse < this.onceTimeout && this.getOtherCondiction()) {
            // 等待时间
            sleep(this.onceTimeout - hasUse)
        }
        log(ctx.label)
        this.logger(`【${ctx.name}】观看结束`)
        if (ctx.label && ctx.like) {
            if (ctx.label.split('#').some(keyword => keywords.includes(keyword))) {
                ctx.like()
                this.logger(`【${ctx.name}】点赞成功`)
                sleep(1000)
            }
        }

    }
    auto(pkg: string) {
        launchPackage(pkg)
        sleep(1000 * 10)
        toTabLayout(pkg, 20)
        toHomeJingXuan()
        this.logger(`开始养机`)
        this.startTime = new Date().getTime();
        while (!this.isFinish() && this.runing && this.getOtherCondiction()) {
            this.onceStartTime = new Date().getTime();
            this.getOtherCondiction() && this.once();
            this.getOtherCondiction() && swipeUpV3();
        }
    }
    getOtherCondiction(){
        if(this.that) return  this.that.isRuning;
        return true;
    }
}
